/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.elcom.config;

import com.elcom.util.Constant;
import java.io.File;
import java.io.IOException;
import org.jconfig.Configuration;
import org.jconfig.ConfigurationManager;
import org.jconfig.ConfigurationManagerException;
import org.jconfig.event.FileListener;
import org.jconfig.handler.XMLFileHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Abstract class used for configution. This class uses LogInterface for logging
 *
 * @author Administrator
 */
public abstract class AbstractReloadConfig implements IConfig {

    /**
     *
     */
    protected static final Logger logger = LoggerFactory.getLogger(AbstractReloadConfig.class);

    /** If the config file does not exist in the specified folder, this method will create a default one.
     *  Then, the config system will be loaded and will be set a FileListener
     * @param configFolder The folder where the config file is stored
     * @param configFileName The name of the jConfig file
     * @throws IOException
     * @throws ConfigurationManagerException
     */
    @Override
    public synchronized void reloadConfig(String configFolder, String configFileName) throws IOException, ConfigurationManagerException {

        File file = new File(configFolder);
        if (!file.exists()) {
            file.mkdirs();
        }

        file = new File(configFolder + configFileName);
        if (!file.exists()) {
            createDefaultConfigFile(file);
        }

        XMLFileHandler fileHandler = new XMLFileHandler();
        fileHandler.setFile(file);
        FileListener fileListener = ConfigFileListener.getInstance();
        fileHandler.addFileListener(fileListener);

        ConfigurationManager cm = ConfigurationManager.getInstance();
        cm.load(fileHandler, Constant.SYS_CONFIG_NAME);
        logConfigValues();
    }

    /**Creatse a default jConfig file
     *
     * @param fout The output file
     * @throws IOException Thrown when the file cannot be created
     */
    @Override
    public abstract void createDefaultConfigFile(File fout) throws IOException;
    //        BufferedWriter w;
    //        try {
    //            w = new BufferedWriter(new FileWriter(fout));
    //            w.write("<?xml version=\"1.0\" ?>");
    //            w.newLine();
    //            w.write("\t<properties>");
    //            w.newLine();
    //            w.write("\t\t<category name=\"" + Constant.CAT_DB_CONNECTION + "\">");
    //            w.newLine();
    //            w.write("\t\t\t<property name=\"" + Constant.PROP_URL + "\" value=\"" + Constant.PROP_URL_DEFAULT + "\"/>");
    //            w.newLine();
    //            w.write("\t\t\t<property name=\"" + Constant.PROP_USERNAME + "\" value=\"" + Constant.PROP_USERNAME_DEFAULT + "\"/>");
    //            w.newLine();
    //            w.write("\t\t\t<property name=\"" + Constant.PROP_PASSWORD + "\" value=\"" + Constant.PROP_PASSWORD_DEFAULT + "\"/>");
    //            w.newLine();
    //            w.write("\t\t\t<property name=\"" + Constant.PROP_SCHEMA + "\" value=\"" + Constant.PROP_SCHEMA_DEFAULT + "\"/>");
    //            w.newLine();
    //            w.write("\t\t\t<property name=\"" + Constant.PROP_MAX_BATCH_UPDATE + "\" value=\"" + Constant.PROP_MAX_BATCH_UPDATE_DEFAULT + "\"/>");
    //            w.newLine();
    //            w.write("\t\t</category>");
    //            w.newLine();
    //            w.write("\t</properties>");
    //            w.flush();
    //            w.close();
    //        } catch (IOException ex) {
    //            ex.printStackTrace();
    //        }
    //    }

    /**
     *
     */
    public void logConfigValues() {
        Configuration config = ConfigurationManager.getConfiguration(Constant.SYS_CONFIG_NAME);
        String[] categories = config.getCategoryNames();

        logger.debug("=====>Current Config<=====");
        for (String category : categories) {
            logger.debug("=====>{} category<=====", category);
            String[] properties = config.getPropertyNames(category);
            for (String property : properties) {
                logger.debug("{}: {}", property, config.getProperty(property, null, category));
            }
        }
        logger.debug("=====>End Current Config<=====");
        logger.debug("");
    }
}
